import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:grocery_app_demo/core/utils/global.dart';
import 'package:grocery_app_demo/pages/drawer/drawer.dart';
import 'package:grocery_app_demo/widgets/gap.dart';

import 'controller.dart';
import 'package:flutter/material.dart';

class SettingPage extends GetView<SettingCtrl> {
  const SettingPage({super.key});

  @override
  Widget build(BuildContext context) {
    Get.put(SettingCtrl());

    return Scaffold(
      appBar: AppBar(
        title: Text('设置'.tr),
      ),
      drawer: const CommonDrawer(),
      body: ConstrainedBox(
        constraints: const BoxConstraints.expand(),
        child: Column(
          children: [
            Card(
              margin: gapAll16,
              child: Padding(
                padding: gapAll16,
                child: Column(
                  children: [
                    buildHookWeightRow(),
                    line,
                    buildWeightUnitRow(),
                    line,

                  ],
                ),
              ),
            ),
            Padding(
              padding: gapAll16,
              child: ElevatedButton(
                onPressed: () => controller.logout(context),
                style: ButtonStyle(
                  shape: MaterialStateProperty.all(
                    RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10.w),
                    ),
                  ),
                ),
                child: Container(
                  alignment: Alignment.center,
                  width: double.infinity,
                  child: Text('退出登陆'.tr),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Row buildWeightUnitRow() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text('默认重量单位'.tr),
        Obx(
          () => Row(
            children: [
              Radio<WeightUnit>(
                  value: WeightUnit.jin,
                  groupValue: controller.weightUnit.value,
                  onChanged: controller.changeWeightUnit),
              Text('斤'.tr),
              Radio<WeightUnit>(
                  value: WeightUnit.kg,
                  groupValue: controller.weightUnit.value,
                  onChanged: controller.changeWeightUnit),
              Text('公斤'.tr),
            ],
          ),
        ),
      ],
    );
  }

  Row buildHookWeightRow() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text('默认去勾重量（Kg）'.tr),
        Row(
          children: [
            SizedBox(
              width: 100.w,
              child: TextField(
                keyboardType: TextInputType.number,
                decoration: const InputDecoration(
                  border: InputBorder.none,
                ),
                inputFormatters: [
                  FilteringTextInputFormatter.allow(RegExp('[0-9.]')),
                ],
                textAlign: TextAlign.center,
                controller: controller.hookWeightCtrl,
              ),
            ),
            SizedBox(
              width: 20.w,
            ),
            ElevatedButton(
              onPressed: controller.saveHookWeight,
              child: Text('保存'.tr),
            ),
          ],
        ),
      ],
    );
  }
}
